home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung CD 2 (Tewi)(1994).iso
/
doc
/
mir
/
hex_bin.c
< prev
next >
Wrap
Text File
|
1992-07-02
|
7KB
|
205 lines
/*
* Usage - hex_bin ascii_input binary_output
*
* HEX_BIN Create a file with any combination of printable and binary
* characters. Used to create test files.
*
* input: An edited ASCII file in which printable characters are as
* desired in output, and binary characters are represented by
* a backslash and two hex values (example, \08 to represent a
* backspace or \5C to represent a backslash).
*
* output: The same file with binary characters replacing all \xx values.
*
* writeup: MIR TUTORIAL ONE, topic 8
*
* Written: Douglas Lowry Mar 05 92
* Copyright (C) 1992 Marpex Inc.
*
* The MIR (Mass Indexing and Retrieval) Tutorials explain detailed
* usage and co-ordination of the MIR family of programs to analyze,
* prepare and index databases (small through gigabyte size), and
* how to build integrated retrieval software around the MIR search
* engine. The fifth of the five MIR tutorial series explains how
* to extend indexing capability into leading edge search-related
* technologies. For more information, GO IBMPRO on CompuServe;
* MIR files are in the DBMS library. The same files are on the
* Canada Remote Systems BBS. A diskette copy of the Introduction
* is available by mail ($10 US... check, Visa or Mastercard);
* diskettes with Introduction, Tutorial ONE software and the
* shareware Tutorial ONE text cost $29. Shareware registration
* for a tutorial is also $29.
*
* E-mail...
* Compuserve 71431,1337
* Internet doug.lowry%canrem.com
* UUCP canrem!doug.lowry
* Others: doug.lowry@canrem.uucp
*
* FAX... 416 963-5677
*
* "Snail mail"... Douglas Lowry, Ph.D.
* Marpex Inc.
* 5334 Yonge Street, #1102
* North York, Ontario
* Canada M2N 6M2
*
* Related database consultation and preparation services are
* available through:
* Innotech Inc., 2001 Sheppard Avenue E., Suite #118,
* North York, Ontario Canada M2J 4Z7
* Tel. 416 492-3838 FAX 416 492-3843
*
* This program is free software; you may redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* (file 05LICENS) along with this program; if not, write to the
* Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
* USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define repeat for(;;)
#define MAX_BYTES 256
/*
* declarations
*/
typedef enum _bool
{ FALSE = 0, TRUE = 1 } Bool;
void Usage_(), process();
char *Cmdname_() { return( "hex_bin" ); }
/*
* MAIN
*/
main( argc, argv )
int argc;
char **argv;
{
FILE *fp, *fp_out ;
char c, c10 ;
long byte_ct, start_at, i ;
c10 = argv[1][0] ;
if( argc != 3 || c10 == '-' || c10 == '/' || c10 == '?' )
Usage_() ;
if(( fp = fopen( argv[1], "r" )) == NULL )
{
fprintf( stderr, "\nUnable to open file %s.\n", argv[1] );
Usage_();
}
if(( fp_out = fopen( argv[2], "wb" )) == NULL )
{
fprintf( stderr, "\nUnable to open file %s.\n", argv[2] );
Usage_();
}
process( fp, fp_out ) ;
fclose( fp );
fclose( fp_out );
exit( 0 );
}
/*
* Usage
*/
void
Usage_()
{
fprintf( stderr,
"\nUsage: %s ascii_input binary_output\n\n\
Create a file with any combination of printable and binary\n\
characters. Used to create test files.\n\n\
input: An edited ASCII file in which printable characters are as\n",
Cmdname_() );
fprintf( stderr,
" desired in output, and binary characters are represented by\n\
a backslash and two hex values (example, \\08 to represent a\n\
backspace or \\5C to represent a backslash).\n\n\
output: The same file with binary characters replacing all \\xx values.\n\n\
writeup: MIR TUTORIAL ONE, topic 8\n\n" ) ;
exit( 1 ) ;
}
/*
* PROCESS
*/
void
process( fp, fp_out )
FILE *fp, *fp_out ;
{
char line_in[ MAX_BYTES ],
c;
int pt, i, len,
nib[2]; /* two consecutive nibbles */
while( fgets( line_in, MAX_BYTES, fp ) != NULL )
{
len = strlen( line_in );
while( line_in[ len-1 ] == '\015' || line_in[ len-1 ] == '\n' )
len-- ;
if( len > MAX_BYTES - 4 )
{
fprintf( stderr, "FATAL... Line exceeds %d bytes.\n\n",
len );
exit( 1 );
}
line_in[ len ] = '\0' ;
for( pt = 0 ; pt < len ; pt++ )
{
if( line_in[ pt ] != '\\' )
fputc( line_in[ pt ], fp_out ) ;
else
{
for( i = 0 ; i < 2 ; i++ )
{
nib[ i ] = -1;
c = line_in[ pt + 1 + i ] ;
if( c >= 0x30 && c <= 0x39 ) /* 0...9*/
nib[ i ] = ( int ) c - 0x30;
else if( c > 0x40 && c < 0x47 ) /* A...F*/
nib[ i ] = 9 + ( int ) c - 0x40;
else if( c > 0x60 && c < 0x67 ) /* a...f*/
nib[ i ] = 9 + ( int ) c - 0x60;
if( nib[ i ] == -1 )
{
fprintf( stderr,
"Non-hex character at byte %d in\n\t%s\n",
pt + 1 + i, line_in );
fclose( fp );
fclose( fp_out ) ;
exit( 1 ) ;
}
}
c = ( char ) ( nib[ 0 ] << 4 | nib[ 1 ] );
fputc( c, fp_out ) ;
pt += 2 ;
}
}
fputc( '\015', fp_out ) ;
fputc( '\n', fp_out ) ;
}
fputc( '\032', fp_out ) ;
return ;
}